package com.actionsoft.Plugin;

import com.actionsoft.bpms.bo.engine.BO;
import com.actionsoft.bpms.org.event.OrgListener;
import com.actionsoft.bpms.org.model.*;
import com.actionsoft.sdk.local.SDK;
import com.oracle.truffle.api.strings.TruffleString;

import java.util.ArrayList;
import java.util.List;

public class Orgoption implements OrgListener {


    @Override
    public boolean beforeCompanyChanged(CompanyModel companyModel, CompanyModel companyModel1, int i) {
        System.out.println("beforeCompanyChanged");
        return true;
    }

    @Override
    public boolean beforeDepartmentChanged(DepartmentModel departmentModel, DepartmentModel departmentModel1, int i) {
        System.out.println("beforeDepartmentChanged");
        return true;
    }

    @Override
    public boolean beforeRoleChanged(RoleModel roleModel, RoleModel roleModel1, int i) {
        System.out.println("beforeRoleChanged");
        return true;
    }

    @Override
    public boolean beforeTeamChanged(TeamModel teamModel, TeamModel teamModel1, int i) {
        System.out.println("beforeTeamChanged");
        return true;
    }

    @Override
    public boolean beforeUserChanged(UserModel userModel, UserModel userModel1, int i) {
        System.out.println("beforeUserChanged");
        return true;
    }

    @Override
    public void afterCompanyChanged(CompanyModel companyModel, CompanyModel companyModel1, int i) {
        System.out.println("afterCompanyChanged");
    }

    @Override
    public void afterDepartmentChanged(DepartmentModel departmentModel, DepartmentModel newModel, int i) {
        System.out.println("afterDepartmentChanged");
        System.out.println(i);
        List<UserModel> managers = SDK.getORGAPI().getManagersByDepartment(newModel.getId());
        String ids = "";
        for (UserModel manager : managers) {
            ids += manager.getUID() + ",";
        }
        BO bo = SDK.getBOAPI().query("BO_EU_XIAOYOUHUIZUZHIXINXI").addQuery("ORG_ID =",newModel.getId()).detail();
        bo.set("USER_ID", ids);
        System.out.println(SDK.getBOAPI().update("BO_EU_XIAOYOUHUIZUZHIXINXI",bo));
    }

    @Override
    public void afterRoleChanged(RoleModel roleModel, RoleModel roleModel1, int i) {
        System.out.println("afterRoleChanged");
    }

    @Override
    public void afterTeamChanged(TeamModel teamModel, TeamModel teamModel1, int i) {
        System.out.println("afterTeamChanged");
    }

    @Override
    public void afterUserChanged(UserModel userModel, UserModel userModel1, int i) {
        System.out.println("afterUserChanged");
    }
}
